#include <GL\glui.h>
#include "Lights.h"
#include "boards/BoardModelling.h"

extern int lights_visible;
extern GLUquadric *glQ;

double symb_light0_radius = 0.2;
int symb_light0_slices = 8;
int symb_light0_stacks =8;

float light_yellow[] = {1.0, 1.0, 0.0};
float light_white[] = {1.0, 1.0, 1.0};
float light_board_position[]  = {0.0, 0.0, 0.0, 1.0};

float light0_ambient[] =   {0.0, 0.0, 0.0, 1.0};
float light0_diffuse[] =   {6.0, 6.0, 6.0, 1.0};
float light0_specular[] =  {6.0, 6.0, 6.0, 1.0};
float light0_kc = 0.0;
float light0_kl = 1.0;
float light0_kq = 0.0;

float spotlight_ambient[] =   {0.0, 0.0, 0.0, 1.0};
float spotlight_diffuse[] =   {10.0, 10.0, 10.0, 1.0};
float spotlight_specular[] =  {5.0, 5.0, 5.0, 1.0};
float spotlight_kc = 0.0;
float spotlight_kl = 1.0;
float spotlight_kq = 0.0;

float light0_position[]  = {BOARD_DIM_X/2, BOARD_DIM_Y * LIGHTS_HEIGHT, -BOARD_DIM_Z/2, 1.0};
float light0_direction[]  = {-BOARD_DIM_X/2, -BOARD_DIM_Y * LIGHTS_HEIGHT, -BENCH_BOARD_TRANSLATE_Z + BOARD_DIM_Z/2, 1.0};

float light1_position[]  = {-BOARD_DIM_X/2, BOARD_DIM_Y * LIGHTS_HEIGHT, -BOARD_DIM_Z/2, 1.0};
float light1_direction[]  = {BOARD_DIM_X/2, -BOARD_DIM_Y * LIGHTS_HEIGHT, BOARD_DIM_Z/2, 1.0};

float light2_position[]  = {BOARD_DIM_X/2, BOARD_DIM_Y * LIGHTS_HEIGHT, -BOARD_DIM_Z/2, 1.0};
float light2_direction[]  = {-BOARD_DIM_X/2, -BOARD_DIM_Y * LIGHTS_HEIGHT, +BOARD_DIM_Z/2, 1.0};

float light3_position[]  = {-BOARD_DIM_X/2, BOARD_DIM_Y * LIGHTS_HEIGHT, BOARD_DIM_Z/2, 1.0};
float light3_direction[]  = {BOARD_DIM_X/2, -BOARD_DIM_Y * LIGHTS_HEIGHT, -BOARD_DIM_Z/2, 1.0};

float light4_position[]  = {BOARD_DIM_X/2, BOARD_DIM_Y * LIGHTS_HEIGHT, BOARD_DIM_Z/2, 1.0};
float light4_direction[]  = {-BOARD_DIM_X/2, -BOARD_DIM_Y * LIGHTS_HEIGHT, -BOARD_DIM_Z/2, 1.0};

float light5_position[]  = {-BOARD_DIM_X/2, BOARD_DIM_Y * LIGHTS_HEIGHT, BOARD_DIM_Z/2, 1.0};
float light5_direction[]  = {BOARD_DIM_X/2, -BOARD_DIM_Y * LIGHTS_HEIGHT, BENCH_BOARD_TRANSLATE_Z - BOARD_DIM_Z/2, 1.0};

float light6_position[]  = {BOARD_DIM_X/2, BOARD_DIM_Y * LIGHTS_HEIGHT, BOARD_DIM_Z/2, 1.0};
float light6_direction[]  = {-BOARD_DIM_X/2, -BOARD_DIM_Y * LIGHTS_HEIGHT, BENCH_BOARD_TRANSLATE_Z - BOARD_DIM_Z/2, 1.0};

float light7_position[]  = {-BOARD_DIM_X/2, BOARD_DIM_Y * LIGHTS_HEIGHT, -BOARD_DIM_Z/2, 1.0};
float light7_direction[]  = {BOARD_DIM_X/2, -BOARD_DIM_Y * LIGHTS_HEIGHT, -BENCH_BOARD_TRANSLATE_Z + BOARD_DIM_Z/2, 1.0};

void init_board_lights(int GL_LIGHT, float light_ambient[4], float light_diffuse[4], float light_specular[4],
						float light_kc, float light_kl, float light_kq) {

	glLightfv(GL_LIGHT, GL_AMBIENT, light_ambient);
	glLightfv(GL_LIGHT, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT, GL_SPECULAR, light_specular);
	glLightf(GL_LIGHT, GL_CONSTANT_ATTENUATION,  light_kc);
	glLightf(GL_LIGHT, GL_LINEAR_ATTENUATION,    light_kl);
	glLightf(GL_LIGHT, GL_QUADRATIC_ATTENUATION, light_kq);
}

void display_board_spotlights(int LIGHT_ID, float light_position[], float light_direction[] ) {

	glLightfv(LIGHT_ID, GL_POSITION, light_position);
	glLightfv(LIGHT_ID, GL_SPOT_DIRECTION, light_direction);
	glLightf(LIGHT_ID, GL_SPOT_CUTOFF, SPOTLIGHT_ARC); 
}

void display_all_lights_spheres() {

	display_lights_sphere(light_yellow, light0_position);
	display_lights_sphere(light_white, light1_position);
	display_lights_sphere(light_white, light2_position);
	display_lights_sphere(light_white, light3_position);
	display_lights_sphere(light_white, light4_position);
	display_lights_sphere(light_white, light5_position);
	display_lights_sphere(light_white, light6_position);
	display_lights_sphere(light_white, light7_position);
}

void display_all_spotlights() {

	display_board_spotlights(GL_LIGHT0, light0_position, light0_direction);
	display_board_spotlights(GL_LIGHT1, light1_position, light1_direction);
	display_board_spotlights(GL_LIGHT2, light2_position, light2_direction);
	display_board_spotlights(GL_LIGHT3, light3_position, light3_direction);
	display_board_spotlights(GL_LIGHT4, light4_position, light4_direction);
	display_board_spotlights(GL_LIGHT5, light5_position, light5_direction);
	display_board_spotlights(GL_LIGHT6, light6_position, light6_direction);
	display_board_spotlights(GL_LIGHT7, light7_position, light7_direction);
}

void init_all_board_lights() {

	init_board_lights(GL_LIGHT0, spotlight_ambient, spotlight_diffuse, spotlight_specular, spotlight_kc, spotlight_kl, spotlight_kq);
	init_board_lights(GL_LIGHT1, spotlight_ambient, spotlight_diffuse, spotlight_specular, spotlight_kc, spotlight_kl, spotlight_kq);
	init_board_lights(GL_LIGHT2, spotlight_ambient, spotlight_diffuse, spotlight_specular, spotlight_kc, spotlight_kl, spotlight_kq);
	init_board_lights(GL_LIGHT3, spotlight_ambient, spotlight_diffuse, spotlight_specular, spotlight_kc, spotlight_kl, spotlight_kq);
	init_board_lights(GL_LIGHT4, spotlight_ambient, spotlight_diffuse, spotlight_specular, spotlight_kc, spotlight_kl, spotlight_kq);
	init_board_lights(GL_LIGHT5, spotlight_ambient, spotlight_diffuse, spotlight_specular, spotlight_kc, spotlight_kl, spotlight_kq);
	init_board_lights(GL_LIGHT6, spotlight_ambient, spotlight_diffuse, spotlight_specular, spotlight_kc, spotlight_kl, spotlight_kq);
	init_board_lights(GL_LIGHT7, spotlight_ambient, spotlight_diffuse, spotlight_specular, spotlight_kc, spotlight_kl, spotlight_kq);
}

void display_lights_sphere(float color[], float light_position[]) {
	if (!lights_visible)
		return;
		
	glColor3f(color[0], color[1], color[2]);		
	gluQuadricOrientation( glQ, GLU_INSIDE);
	glPushMatrix();
	glTranslated(light_position[0], light_position[1], light_position[2]);
	gluSphere(glQ, symb_light0_radius, symb_light0_slices, symb_light0_stacks);
    glPopMatrix();
	gluQuadricOrientation( glQ, GLU_OUTSIDE);
}